package photogcalc.calc;

import photogcalc.util.MathUtil;

/** Calculates the angle of view and the field of view.
 *  All calculations are done using metric system and millimetres.
 *  @version 2008-06-10
 *  @author Antti Okkonen
*/
public class AngleOfView {
    protected double width, height;

    /** Default constructor.Default size is the
     *  size of the 35mm film frame.
     */
    public AngleOfView() {
        this(36, 24); // 35mm film
    }

    /** Default constructor with sensors / films size.
     *
     * @param w width
     * @param h height
     */
    public AngleOfView(double w, double h) {
        width = w;
        height = h;
    }

    private double getWidth() {
        return width;
    }

    private double getHeight() {
        return height;
    }

    /** Calculates horizontal angle of view.
     *  @param focalLength focal length
     *  @return angle
     */
    public double calculateHorizontalAngle(double focalLength) {
        return 2 * Math.toDegrees(MathUtil.cordic_atan(getWidth() / (2 * focalLength)));
    }

    /** Calculates vertical angle of view.
    @param focalLength focal length
    @return angle
     */
    public double calculateVerticalAngle(double focalLength) {
        return 2 * Math.toDegrees(MathUtil.cordic_atan(getHeight() / (2 * focalLength)));
    }

    /** Calculates diagonal angle of view.
    @param focalLength focal length
    @return angle
     */
    public double calculateDiagonalAngle(double focalLength) {
        double diagonal = Math.sqrt(getHeight() * getHeight() + getWidth() * getWidth());
        double tmp = diagonal / (2 * focalLength);
        double radians = MathUtil.cordic_atan(tmp);
        double degrees = 2 * Math.toDegrees(radians);
        return degrees;
    }

    /** Calculates the width of the picture area. The formula
     *  is <i>width = (sensor width / focallength) * distance in meters</i>
     *
     * @param distance
     * @param focalLength
     * @return field of view
     */
    public double calculateWidth(double distance, double focalLength) {
        return (this.width / focalLength) * distance;
    }

    /** Calculates the width of the picture area. The formula
     *  is <i>width = (sensor width / focallength) * distance in meters</i>
     *
     * @param distance
     * @param focalLength
     * @return field of view
     */
    public double calculateHeight(double distance, double focalLength) {
        return (this.height / focalLength) * distance;
    }
}

